package com.test38_集合;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * Set集合，保存的数据不允许重复
 */
public class Demo02_Set {

    /**
     * Set接口
     */
    @Test
    public void test1() {
        {
            String[] strArr = {"www.yootk.com", "www.yootk.com", "edu.yootk.com"};
            Set<String> all = Arrays.stream(strArr).collect(Collectors.toSet());
            System.out.println(all);
        }
        {
            // JDK1.9支持
            Set<String> all = Set.of("www.yootk.com", "edu.yootk.com", "yootk.ke.qq.com");
            System.out.println(all);
        }
    }

    /**
     * HashSet，散列存储
     */
    @Test
    public void test2() {
        Set<String> all = new HashSet<>();
        all.add("www.yootk.com");
        all.add("www.yootk.com");   // 重复内容，不保存
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        System.out.println(all);
    }

    /**
     * LinkedHashSet，按保存顺序存储
     */
    @Test
    public void test3() {
        Set<String> all = new LinkedHashSet<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        System.out.println(all);
    }

    /**
     * TreeSet，有序存储
     */
    @Test
    public void test4() {
        Set<String> all = new TreeSet<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        System.out.println(all);
    }

    @Test
    public void test5() {
        Set<Book2> all = new TreeSet<>();
        all.add(new Book2("Java从入门到项目实战", "李兴华", 99.8));
        all.add(new Book2("Java从入门到项目实战", "李兴华", 99.8));    // 数据重复
        // compareTo()返回的是0，则认为是相同的元素，不会往里面添加
        all.add(new Book2("Vue.JS从入门到项目实战", "李兴华", 99.8));
        all.add(new Book2("Python从入门到项目实战", "李兴华", 97.9));
        all.add(new Book2("Go从入门到项目实战", "李兴华", 98.6));
        all.forEach(System.out::println);
    }

    /**
     * 重复元素判断
     */
    @Test
    public void test6() {
        Book2 bookA = new Book2("Java从入门到项目实战", "李兴华", 99.8);
        Book2 bookB = new Book2("Java从入门到项目实战", "李兴华", 99.8);
        System.out.println(bookA.hashCode() + "=========" + bookB.hashCode());
        System.out.println(bookA.hashCode() == bookB.hashCode());
        System.out.println(bookA.equals(bookB));
    }
}

class Book2 implements Comparable<Book2> {
    private String title;
    private String author;
    private double price;

    public Book2(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "【Book】图书名称：" + this.title + "、作者：" + this.author + "、价格：" + this.price;
    }

    @Override
    public int compareTo(Book2 o) {
        if (this.price > o.price) {
            return 1;
        } else if (this.price < o.price) {
            return -1;
        }
        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book2 book2 = (Book2) o;
        return Double.compare(book2.price, price) == 0 && title.equals(book2.title) && author.equals(book2.author);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, author, price);
    }
}
